<template>
  <div class="prescription-container">
    <register ref="register" class="prescription-register-container" @selectRegister="selectRegister" />
    <prescription ref="pres" class="prescription" :data="form" @deleteRow="deleteRow" @quantityChange="quantityChange" @afterSavePres="afterSavePres" />
    <drug class="prescription-drug-container" @selectDrug="selectDrug" />
  </div>
</template>

<script>
import register from '../components/register/index'
import prescription from '../components/prescription/index'
import drug from '../components/drug/index'
import { formatDate } from '@/utils/date'
import { mapGetters } from 'vuex'
export default {
  name: 'PrescriptionNew',
  components: { register, prescription, drug },
  data() {
    return {
      form: {
        pres_id: '',
        amount: 0,
        pres_date: formatDate(new Date(), 'Y-M-D h:m:s'),
        print: false
      },
      form1: {
        patient_id: 0,
        patient_name: '',
        patient_sex: '',
        patient_age: '',
        patient_contact_tel: '',
        patient_addr: '',
        doctor_name: '',
        sect_name: '',
        pres_date: '2024-01-01',
        pres_id: 'NewNO',
        pres_type: '',
        charge_cat_id: 1,
        amount: '0',
        diag_id: 101280,
        list: [{
          medi_mark_id: '',
          name: '甲 孟鲁司特钠片',
          spec: '10mg*12s/鲁南贝特',
          pack: 1,
          unit_id: 4,
          unit_name: '盒',
          quantity: 10,
          resale_price: 3.8,
          amount: 38,
          medi_pass: 5,
          medi_pass_name: '口服',
          use_type_id: 3,
          use_type_name: '一天三次',
          medi_doage: 1,
          medi_unit_id: 5,
          medi_unit_name: '粒',
          medi_use: '1粒/次，口服，一天三次'
        }],
        print: false,
        view: false
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    selectRegister(data) {
      this.form.print = false
      this.form.register_pres = data.pres_id
      this.form.pres_id = ''
      this.form.patient_id = data.patient_id
      this.form.patient_name = data.patient_name
      this.form.patient_sex = data.patient_sex
      this.form.patient_age = data.patient_age
      this.form.patient_card_nbr = data.patient_card_nbr
      this.form.patient_contact_tel = data.patient_contact_tel
      this.form.patient_addr = data.patient_addr
      this.form.sect_id = this.userInfo.sect_id
      this.form.sect_name = this.userInfo.sect_name
      this.form.doct_id = this.userInfo.staff_id
      this.form.doctor_name = this.userInfo.name
      this.form.pres_date = formatDate(new Date(), 'Y-M-D h:m:s')
      this.form.charge_cat_id = 1
      this.form.pres_count = 1
      this.form.icds = []
      this.form.sts = 'C'
      this.form.amount = 0
      this.form.list = []
      this.form.valid_days = 7
      this.form.hi_rxno = ''
      this.form.rx_trace_code = ''
      this.form.rx_stas_codg = ''
      this.$refs.pres.initDiagnose(this.form.icds)
    },
    selectDrug(drug) {
      if (!this.form.patient_id) {
        this.$alert('请先选择患者信息')
      } else {
        const item = JSON.parse(JSON.stringify(drug))
        item.quantity = 1
        item.zxts = 1
        item.amount = item.quantity * item.resale_price
        this.form.list.push(item)
        this.summary()
      }
    },
    summary() {
      this.form.amount = parseFloat(this.form.list.reduce((acc, cur) => acc + cur.amount, 0).toFixed(2))
      this.$forceUpdate()
    },
    deleteRow({ index, row }) {
      // this.$alert(index)
      this.form.list.splice(index, 1)
      this.summary()
    },
    quantityChange({ index, row }) {
      row.amount = row.quantity * row.resale_price
      this.summary()
    },
    afterSavePres(data) {
      this.$refs.register.setRegister({ pres_id: data.register_pres, sts: 'C' })
    }
  }
}
</script>

<style lang="scss" scoped>
.prescription-container {
  display: flex;
  height: calc(100vh - 6rem);
  overflow-y: hidden;
  .prescription-register-container {
    width: 15%;
    text-align: center;
    padding: 1rem;
    font-size: 1.5rem;
    height: 100%;
  }
  .prescription{
    width: 60%;
    padding: 1rem;
    margin: 0 1rem;
    overflow-y: auto;
    height: 100%;
  }
  .prescription-drug-container {
    width: 20%;
    text-align: center;
    padding: 1rem 0;
    font-size: 1.5rem;
    height: 100%;
  }
}
</style>
